package com.tencent.sr.iris.basic.mp.dependency.user;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.tencent.sr.iris.basic.mp.common.util.HttpUtils;
import com.tencent.sr.iris.user.core.service.interfaces.client.activity.IrisUserActivityClient;
import com.tencent.sr.iris.user.core.service.interfaces.client.level.LevelClient;
import com.tencent.sr.iris.user.core.service.interfaces.request.activity.IrisUserBatchQueryRequest;
import com.tencent.sr.iris.user.core.service.interfaces.request.level.UserLevelBatchRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.activity.IrisUserInfo;
import com.tencent.sr.iris.user.core.service.interfaces.response.activity.UserBatchInfoResponse;
import com.tencent.sr.iris.user.core.service.interfaces.response.level.UserLevelBatchResponse;
import com.tencent.sr.iris.user.core.service.interfaces.response.level.UserLevelResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Component
public class UserDependency {

    @Resource
    private AppUser appUser;

    @Value("${iris.user.max.batch.size:100}")
    private int maxSize;

    @Resource
    private LevelClient levelClient;

    @Resource
    private IrisUserActivityClient irisUserActivityClient;

    /**
     * 批量查询用户等级
     * @param request
     * @return
     */
    public List<UserLevelResponse> queryBatchUserLevel(UserLevelBatchRequest request) {
        request.setSaasId(String.valueOf(appUser.getSaasId()));

        log.info("调用UserDependency.queryBatchUserLevel，请求参数：{}", JSON.toJSONString(request));
        List<String> uidList = request.getUidList();
        ArrayList<UserLevelResponse> resList = Lists.newArrayListWithCapacity(uidList.size());
        List<List<String>> partition = Lists.partition(uidList, maxSize);
        for (List<String> list : partition) {
            request.setUidList(list);
            resList.addAll(doQueryBatchUserLevel(request));
        }

        log.info("调用UserDependency.queryBatchUserLevel，请求参数：{}，请求结果：{}",
                JSON.toJSONString(request), JSON.toJSONString(resList));
        return resList;
    }

    /**
     * 批量查询用户信息
     * @param request
     * @return
     */
    public List<IrisUserInfo> batchQueryUserInfo(IrisUserBatchQueryRequest request) {
        log.info("调用UserDependency.batchQueryUserInfo，请求参数：{}", JSON.toJSONString(request));

        List<String> uidList = request.getUidList();
        ArrayList<IrisUserInfo> resList = Lists.newArrayListWithCapacity(uidList.size());
        List<List<String>> partition = Lists.partition(uidList, maxSize);

        for (List<String> list : partition) {
            request.setUidList(list);
            resList.addAll(doBatchQueryUserInfo(request));
        }

        log.info("调用UserDependency.batchQueryUserInfo，请求参数：{}，请求结果：{}",
                JSON.toJSONString(request), JSON.toJSONString(resList));
        return resList;
    }

    private List<UserLevelResponse> doQueryBatchUserLevel(UserLevelBatchRequest request) {
        HttpResult<UserLevelBatchResponse> result = levelClient.queryBatchUserLevel(request);
        UserLevelBatchResponse response = HttpUtils.handleError(result);
        if (response == null || CollectionUtil.isEmpty(response.getUserLevelResponseList())) {
            return Collections.emptyList();
        }

        return response.getUserLevelResponseList();
    }

    private List<IrisUserInfo> doBatchQueryUserInfo(IrisUserBatchQueryRequest request) {
        log.info("调用UserDependency.batchQueryUserInfo，请求参数：{}", JSON.toJSONString(request));
        HttpResult<UserBatchInfoResponse> result = null;
        try {
            result = irisUserActivityClient.batchQueryUserInfo(request);
        } catch (Exception e) {
            log.error("UserDependency.batchQueryUserInfo请求失败：", e);
        }

        UserBatchInfoResponse response = HttpUtils.handleError(result);
        if (response == null || CollectionUtil.isEmpty(response.getUserInfoList())) {
            return Collections.emptyList();
        }

        List<IrisUserInfo> list = response.getUserInfoList();
        log.info("调用UserDependency.batchQueryUserInfo，请求参数：{}，请求结果：{}",
                JSON.toJSONString(request), JSON.toJSONString(list));
        return list;
    }
}
